package com.batchbrake.buttons;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;

public class SkinnableButton extends JButton implements MouseListener {
	private ImageIcon[] disabled = new ImageIcon[] {
			new ImageIcon("images/encode/PanelButtonDisabledLeft.png"),
			new ImageIcon("images/encode/PanelButtonDisabledMiddle.png"),
			new ImageIcon("images/encode/PanelButtonDisabledRight.png")
	};
	private ImageIcon[] normal = new ImageIcon[] {
			new ImageIcon("images/encode/PanelButtonNormalLeft.png"),
			new ImageIcon("images/encode/PanelButtonNormalMiddle.png"),
			new ImageIcon("images/encode/PanelButtonNormalRight.png")
	};
	private ImageIcon[] over = new ImageIcon[] {
			new ImageIcon("images/encode/PanelButtonOverLeft.png"),
			new ImageIcon("images/encode/PanelButtonOverMiddle.png"),
			new ImageIcon("images/encode/PanelButtonOverRight.png")
	};
	private ImageIcon[] pushed = new ImageIcon[] {
			new ImageIcon("images/encode/PanelButtonPushedLeft.png"),
			new ImageIcon("images/encode/PanelButtonPushedMiddle.png"),
			new ImageIcon("images/encode/PanelButtonPushedRight.png")
	};
	
	private boolean isOver = false;
	private boolean isClicked = false;
	
	public SkinnableButton(String title, ImageIcon[] disabled, ImageIcon[] normal, ImageIcon[] over, ImageIcon[] pushed) {
		super(title);
		this.addMouseListener(this);
		this.setFont(getFont().deriveFont(Font.BOLD, 11f));
		this.disabled = disabled;
		this.normal = normal;
		this.over = over;
		this.pushed = pushed;
	}
	
	
	public void setIcons(ImageIcon[] disabled, ImageIcon[] normal, ImageIcon[] over, ImageIcon[] pushed) {
		this.disabled = disabled;
		this.normal = normal;
		this.over = over;
		this.pushed = pushed;
		repaint();
	}

	@Override
	public Dimension getPreferredSize() {
		Dimension dim = super.getPreferredSize();
		dim.setSize(dim.width + 15, 18);
		return dim;
	}


	@Override
	public void paintComponent(Graphics graphic) {
		Graphics2D g = (Graphics2D) graphic;
		ImageIcon[] toPaint = null;
		
		if( isOver ) {
			if( isClicked ) {
				toPaint = pushed;
			} else {
				toPaint = over;
			}
		} else {
			toPaint = normal;
		}
		
		int height = toPaint[0].getIconHeight() + 2;
		int top = (this.getHeight() - height) / 2;
		int width = this.getWidth() - 5;
		
		g.addRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
		g.addRenderingHints(new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON));
		
		g.drawImage(toPaint[0].getImage(), 0, top, toPaint[0].getIconWidth(), height, null);
		g.drawImage(toPaint[1].getImage(), toPaint[0].getIconWidth(), top, width - toPaint[2].getIconWidth() - toPaint[0].getIconWidth(), height, null);
		g.drawImage(toPaint[2].getImage(), width - toPaint[2].getIconWidth(), top, toPaint[0].getIconWidth(), height, null);

		int strHeight = getFontMetrics(getFont()).getHeight();
		
//		g.setColor(isClicked ? Color.black : Color.white);
		int fontSize = g.getFont().getSize();
		
		g.setColor(new Color(0f,0f,0f,0.3f));
		g.drawString(this.getText(), 11, top + strHeight + 1);
		g.drawString(this.getText(), 9, top + strHeight - 1);
		g.drawString(this.getText(), 11, top + strHeight - 1);
		g.drawString(this.getText(), 9, top + strHeight + 1);
//		g.setColor(isClicked ? Color.white : Color.black);
//		g.setFont(g.getFont().deriveFont(fontSize));
		g.setColor(Color.white);
		g.drawString(this.getText(), 10, top + strHeight);
//		super.paintComponent(graphic);
	}


	@Override
	public void mouseClicked(MouseEvent arg0) {
		isClicked = true;
	}


	@Override
	public void mouseEntered(MouseEvent arg0) {
		isOver = true;
	}


	@Override
	public void mouseExited(MouseEvent arg0) {
		isOver = false;
		isClicked = false;
	}


	@Override
	public void mousePressed(MouseEvent arg0) {
		isClicked = true;
		isOver = true;
	}


	@Override
	public void mouseReleased(MouseEvent arg0) {
		isClicked = false;
		repaint();
	}
}
